package cn.seecoder.ai.controller;

import cn.seecoder.ai.model.vo.DeploymentVO;
import cn.seecoder.ai.model.vo.PredictionVO;
import cn.seecoder.ai.model.vo.ResultVO;
import cn.seecoder.ai.service.DeploymentService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author fanyanpeng
 * @date 2023/4/16 23:19
 */
@Slf4j
@RestController
@RequestMapping("${apiPrefix}/ml/deployments")
public class DeploymentController {

    @Autowired
    DeploymentService deploymentService;


    @ApiOperation("部署模型")
    @PostMapping("")
    public ResultVO<DeploymentVO> deployModel(@RequestBody DeploymentVO deploymentVO){
        deploymentVO = deploymentService.renewDeploymentVO(deploymentVO);
        deploymentService.asyncDeploy(deploymentVO.getId());
        return ResultVO.buildSuccess(deploymentVO);
    }


    @ApiOperation("查看用户部署情况")
    @GetMapping("")
    public ResultVO<List<DeploymentVO>> getDeploymentVOList(@RequestParam Integer userId){
        return ResultVO.buildSuccess(deploymentService.getDeploymentVOList(userId));

    }

    @ApiOperation("查看某个模型的部署情况，包括历史数据")
    @GetMapping("/models/{modelId}")
    public ResultVO<List<DeploymentVO>> getDeploymentVOListByModelId(@RequestParam Integer userId,
                                                            @PathVariable Integer modelId){
        return ResultVO.buildSuccess(deploymentService.getDeploymentVOList(userId,modelId));
    }


    @ApiOperation("查看某一次部署情况")
    @GetMapping("/{deploymentId}")
    public ResultVO<DeploymentVO> getDeploymentVO(@RequestParam Integer userId,
                                                  @PathVariable Integer deploymentId){
        return ResultVO.buildSuccess(deploymentService.getDeploymentVO(userId,deploymentId));
    }

    @ApiOperation("触发重新部署")
    @GetMapping("/{deploymentId}/redeploy")
    public ResultVO<DeploymentVO> reDeploy(@RequestParam Integer userId,
                                                  @PathVariable Integer deploymentId){
        return ResultVO.buildSuccess(deploymentService.reDeploy(userId,deploymentId));
    }

    @ApiOperation("触发取消部署")
    @GetMapping("/{deploymentId}/undeploy")
    public ResultVO<DeploymentVO> unDeploy(@RequestParam Integer userId,
                                           @PathVariable Integer deploymentId){
        return ResultVO.buildSuccess(deploymentService.unDeploy(userId,deploymentId));
    }

    @ApiOperation("使用部署")
    @GetMapping("/{deploymentId}/use")
    public ResultVO<PredictionVO> useDeployment(@RequestParam Integer userId, @RequestParam Integer fileInfoId,
                                                @PathVariable Integer deploymentId){
        return ResultVO.buildSuccess(deploymentService.useDeployedModel(userId,deploymentId,fileInfoId));
    }

    @ApiOperation("使用部署")
    @PostMapping("/{deploymentId}/use-with-file")
    public ResultVO<PredictionVO> useDeployment(@PathVariable Integer deploymentId,@RequestParam MultipartFile file){
        return ResultVO.buildSuccess(deploymentService.useDeployedModelWithFile(deploymentId,file));
    }


}
